﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ChurchData.Business;

namespace ChurchData
{
    public partial class PeopleMain : ChurchForm
    {
        private int PeoplePK { get; set; }

        public PeopleMain(int peoplePK)
        {
            InitializeComponent();

            this.PeoplePK = peoplePK;
            this.Title = "(" + peoplePK.ToString() + ")";
            this.lblSaved.Text = "";
        }

        private void PeopleMain_Load(object sender, EventArgs e)
        {
            this.peopleBasicUC1.LoadPersonInfo(this.PeoplePK);
            this.Text = peopleBasicUC1.FirstName + " " + peopleBasicUC1.LastName;
            this.Text += " - Entry# " + PeoplePK;

            this.peopleSecureNotesUC1.LoadPersonInfo(this.PeoplePK);
            this.peopleKeywordsUC1.LoadPersonInfo(this.PeoplePK);
            this.peopleDatesUC1.LoadPersonInfo(this.PeoplePK);
            this.peopleContactUC1.LoadPersonInfo(this.PeoplePK);
            this.peopleRelationsUC1.LoadPersonInfo(this.PeoplePK);

            if (ChurchData.Business.UserInformation.ViewOnlyInd)
            {
                this.btnSave.Enabled = false;
                this.btnDelete.Enabled = false;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (peopleBasicUC1.HasUnsavedChanges)
                peopleBasicUC1.SaveIt();

            if (peopleSecureNotesUC1.HasUnsavedChanges)
                peopleSecureNotesUC1.SaveIt();

            if (peopleContactUC1.HasUnsavedChanges)
                peopleContactUC1.SaveIt();

            this.lblSaved.Text = "Saved at " + DateTime.Now;   
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (DialogResult.Yes == MessageBox.Show("WOH! Are you sure?", "Confirm Deletion", MessageBoxButtons.YesNo))
            {
                PeopleBO pBO = new PeopleBO();
                pBO.DeletePerson(this.PeoplePK);
                this.Close();
            }
        }

        private void PeopleMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (peopleBasicUC1.HasUnsavedChanges
                || peopleSecureNotesUC1.HasUnsavedChanges
                || peopleContactUC1.HasUnsavedChanges)
            {
                DialogResult result = MessageBox.Show("Would you like to save your changes?", "Changes not saved", MessageBoxButtons.YesNoCancel);
                if (result == DialogResult.Cancel) e.Cancel = true;
                else if (result == DialogResult.Yes) btnSave.PerformClick();
            }
        }
    }
}
